package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.system.domain.BsLotteryHistory;
import com.ruoyi.system.domain.SysElectronic;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.BsLottery;
import com.ruoyi.system.service.IBsLotteryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 抽奖号码Controller
 * 
 * @author ruoyi
 * @date 2022-12-25
 */
@RestController
@RequestMapping("/system/lottery")
public class BsLotteryController extends BaseController
{
    @Autowired
    private IBsLotteryService bsLotteryService;

    /**
     * 查询抽奖号码列表
     */
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(BsLottery bsLottery)
    {
        startPage();
        List<BsLottery> list = bsLotteryService.selectBsLotteryList(bsLottery);
        return getDataTable(list);
    }

    /**
     * 导出抽奖号码列表
     */
    @PreAuthorize("@ss.hasPermi('system:lottery:export')")
    @Log(title = "抽奖号码", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BsLottery bsLottery)
    {
        List<BsLottery> list = bsLotteryService.selectBsLotteryList(bsLottery);
        ExcelUtil<BsLottery> util = new ExcelUtil<BsLottery>(BsLottery.class);
        util.exportExcel(response, list, "抽奖号码数据");
    }
    /**
     * 导入抽奖号码列表
     */
    @Log(title = "抽奖管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<BsLottery> util = new ExcelUtil<BsLottery>(BsLottery.class);
        List<BsLottery> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = bsLotteryService.importUser(userList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<BsLottery> util = new ExcelUtil<BsLottery>(BsLottery.class);
        util.importTemplateExcel(response,"监控数据");
    }

    /**
     * 获取抽奖号码详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:lottery:query')")
    @GetMapping(value = "/{lotteryId}")
    public AjaxResult getInfo(@PathVariable("lotteryId") Long lotteryId)
    {
        return AjaxResult.success(bsLotteryService.selectBsLotteryByLotteryId(lotteryId));
    }

    /**
     * 新增抽奖号码
     */
    @PreAuthorize("@ss.hasPermi('system:lottery:add')")
    @Log(title = "抽奖号码", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BsLottery bsLottery)
    {
        return toAjax(bsLotteryService.insertBsLottery(bsLottery));
    }

    /**
     * 修改抽奖号码
     */
    @PreAuthorize("@ss.hasPermi('system:lottery:edit')")
    @Log(title = "抽奖号码", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BsLottery bsLottery)
    {
        return toAjax(bsLotteryService.updateBsLottery(bsLottery));
    }

    /**
     * 删除抽奖号码
     */
    @PreAuthorize("@ss.hasPermi('system:lottery:remove')")
    @Log(title = "抽奖号码", businessType = BusinessType.DELETE)
	@DeleteMapping("/{lotteryIds}")
    public AjaxResult remove(@PathVariable Long[] lotteryIds)
    {
        return toAjax(bsLotteryService.deleteBsLotteryByLotteryIds(lotteryIds));
    }

    /**
     * 抽奖
     * @param lotteryNums
     * @return
     */
    @Anonymous
    @RequestMapping("/onLottery/{lotteryNums}")
    public List<String> onLottery(@PathVariable("lotteryNums") Long lotteryNums){
        return bsLotteryService.onLottery(lotteryNums);
    }
}
